﻿using System;
using System.Collections.Generic;
using System.Windows;
using Kiva7.Images;
using Kiva7.Translations;
using Microsoft.Phone.Controls;
using Kiva7.Controls;
using Kiva7.DataClasses;
using Kiva7.Utilities;
using Kiva7.DataRequests;

namespace Kiva7.Pages
{
    public partial class LenderDetailPage
    {

        private const string Url = "/Pages/LenderDetailPage.xaml";
        public readonly static Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);

        public static Uri GetPageUrl(IEnumerable<Lender> lenderList, int index)
        {
            if (lenderList == null)
                return null;

            List<string> list = new List<string>();
            foreach (Lender lender in lenderList)
            {
                list.Add(lender.LenderId);
            }

            UrlCreator url = new UrlCreator(Url);
            url.Add(CacheGuidKey, CacheIdList(list));
            url.Add(IndexKey, index);

            return url.ToUri(UriKind.RelativeOrAbsolute);
        }

        public LenderDetailPage()
        {
            InitializeComponent();
            CreateStandardControls();

            SetPivot(Pivot);
        }

        private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
        {
            _request = new LenderRequest();
            //request.OnResponse += new DataRequest<DataResponse.LoansResponse, Loan>.DataRequestDelegate(request_OnResponse);

            DataContext = _request;
            //lenderControl.DataContext = request;

            _lenderLoansRequest = new LenderLoansRequest();
            loansControl.DataContext = _lenderLoansRequest;

            RequestMain();

            ApplicationBar.IsVisible = IdList.Count > 1;
        }

        LenderRequest _request;
        protected override void RequestMain()
        {
            if (_request.LoadStatus == LoadStatus.NotLoaded)
            {
                _request.GetLender(IdList[Index]);
            }
        }

        LenderLoansRequest _lenderLoansRequest;
        private void RequestLoans()
        {
            if (_lenderLoansRequest.LoadStatus == LoadStatus.NotLoaded)
            {
                _lenderLoansRequest.GetLoans(IdList[Index], SortBy.Newest);
            }
        }

        private void PivotLoadingPivotItem(object sender, PivotItemEventArgs e)
        {
            CheckPivot(e.Item);
        }

        protected override void CheckPivot(PivotItem pi)
        {
            if (_BarButtonTranslate != null)
            {
                _BarButtonTranslate.IsEnabled = pi == Lender;
            }
            if (pi == Loans)
            {
                RequestLoans();
            }
        }

        protected override void ClearRequests()
        {
            _request.Clear();
            _lenderLoansRequest.Clear();
        }

        private BindableApplicationBarButton _BarButtonTranslate;

        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.Back, ImageUrls.Back, BackClick);

            _BarButtonTranslate = AddBarButton(AppResources.TranslateTexts, ImageUrls.Translation, TranslateClick);

            AddBarButton(AppResources.AddAFriend, ImageUrls.Plus, AddFriendClick);

            AddBarButton(AppResources.Next, ImageUrls.Next, NextClick);
        }

        private void AddFriendClick(object sender, EventArgs e)
        {
            DoNavigate(FriendAddPage.GetPageUrl(IdList[Index]));
        }

        private void TranslateClick(object sender, EventArgs e)
        {
            lenderControl.TranslateTextWithDetect();
        }

    }
}